package com.eims.hongbao.fragment;

import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.contans.view.GetHongbaoLsitVo;
import com.eims.sls_app.ui.CirclePersonInfo;
import com.eims.sls_app.ui.HongbaoListInfoActivity;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.view.XCRoundImageView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import bls.juejin.cn.R;

public class HongbaoListInfoAdapter extends BaseAdapter {
	private Context context;
	private static final String TAG = "HongbaoListInfoAdapter";
	private List<GetHongbaoLsitVo> lsitVos;

	public HongbaoListInfoAdapter(Context context, List<GetHongbaoLsitVo> lsitVos) {
		super();
		this.context = context;
		this.lsitVos = lsitVos;

	}

	@Override
	public int getCount() {

		if (lsitVos != null) {
			return lsitVos.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();

			view = LayoutInflater.from(context).inflate(R.layout.hongbao_listview_info_item, null);

			holder.iv_Head = (XCRoundImageView) view.findViewById(R.id.iv_item_head);
			holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
			holder.tv_time = (TextView) view.findViewById(R.id.tv_time);
			holder.tv_amount = (TextView) view.findViewById(R.id.tv_count);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		ImageManager.getInstance().displayImage(App.url + lsitVos.get(position).getHeadurl(), holder.iv_Head,
				ImageManager.getViewsHeadOptions());
		final String name = lsitVos.get(position).getName();
		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(name)) {
			isname = name.substring(0, 3) + "****" + name.substring(7, name.length());

		} else {
			isname = name;

		}

		holder.tv_name.setText(isname);

		holder.tv_time.setText(lsitVos.get(position).getDate());
		Double num = Double.parseDouble(lsitVos.get(position).getMoney());
		DecimalFormat df = new DecimalFormat("######0.00");
		String decimalBalanceString = df.format(num) + "元";// format 返回的是字符
		holder.tv_amount.setText( decimalBalanceString);
		
		holder.iv_Head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("userId", lsitVos.get(position).getUid());

				Intent intent = new Intent(context, CirclePersonInfo.class);
				intent.putExtras(bundle);
				// intent.putExtra("userId", topicBeans.get(position).id+"");
				context.startActivity(intent);
			}
		});

		return view;
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	static class ViewHolder {
		XCRoundImageView iv_Head;
		TextView tv_name;
		TextView tv_time;
		TextView tv_amount;

	}

	public String formateRate(String rateStr) {
		if (rateStr.indexOf(".") != -1) {

			int num = 0;
			num = rateStr.indexOf(".");

			String dianAfter = rateStr.substring(0, num + 1);
			String afterData = rateStr.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			} else {
				afterData = afterData;
			}
			return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
		} else {
			if (rateStr == "1") {
				return "100";
			} else {
				return rateStr;
			}
		}
	}

}
